package com.qilu.pe.ui.activity;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;

import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;
import com.qilu.pe.R;
import com.qilu.pe.base.BaseResult2;
import com.qilu.pe.base.Config;
import com.qilu.pe.base.Global;
import com.qilu.pe.base.MyApp;
import com.qilu.pe.dao.bean.EventModel;
import com.qilu.pe.dao.bean.SettingsData;
import com.qilu.pe.dao.http.APIRetrofit;
import com.qilu.pe.support.util.ActiviceCollector;
import com.qilu.pe.support.util.AlertUtil;
import com.qilu.pe.support.util.Logg;
import com.qilu.pe.support.util.ShareUtils;
import com.ruitu.arad.Arad;
import com.ruitu.arad.api.RxUtil;

import java.util.List;

import io.reactivex.disposables.Disposable;
import io.reactivex.functions.Consumer;

public class LauncherActivity  extends AppCompatActivity {

    /**
     * 1.延时2000ms
     * 2.判断程序是否第一次运行
     * 3.Activity全屏主题
     */

    //闪屏页延时
    private static final int HANDLER_SPLASH = 1001;
    //判断程序是否是第一次运行
    private static final String SHARE_IS_FIRST = "isFirst";


    private Handler handler = new Handler(new Handler.Callback() {
        @Override
        public boolean handleMessage(Message msg) {
            switch (msg.what) {
                case HANDLER_SPLASH:
                    //判断程序是否是第一次运行
                    if (isFirst()) {
                        startActivity(new Intent(LauncherActivity.this, GuideActivity.class));
                    } else {
                        startActivity(new Intent(LauncherActivity.this, LoginActivity.class));
                    }
                    finish();
                    break;
                default:
                    break;
            }
            return false;
        }

    });


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_launcher);
        ActiviceCollector.addActivity(this);
        initView();
        getSettings1();
    }
    private Disposable getSettings1() {
        return  APIRetrofit.getDefault().getSettings1()
                .compose(RxUtil.<BaseResult2<List<SettingsData>>>subIoObMain())
                .subscribe(new Consumer<BaseResult2<List<SettingsData>>>() {
                    @Override
                    public void accept(BaseResult2<List< SettingsData >> res) throws Exception {
                        if (res.isSuccess()) {
                            if (res.getData() != null && res.getData().size() > 0) {
                                Arad.preferences.putString(Config.MALL_SWITCH,String.valueOf(res.getData().get(0).getMall_switch())).flush();
//                                MyApp.settings = res.getData().get(0);
//                                Logg.e("===信息== " + MyApp.settings.getExplain());
                            }
                        } else {
                            Arad.preferences.putString(Config.MALL_SWITCH,"1").flush();
                        }
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        Arad.preferences.putString(Config.MALL_SWITCH,"1").flush();
                    }
                });
    }
    //初始化View
    private void initView() {
        //延时2000ms
        handler.sendEmptyMessageDelayed(HANDLER_SPLASH, 2000);
    }

    //判断程序是否第一次运行
    private boolean isFirst() {
        boolean isFirst = ShareUtils.getBoolean(this, SHARE_IS_FIRST, true);
        if (isFirst) {
            ShareUtils.putBoolean(this, SHARE_IS_FIRST, false);
            //是第一次运行
            return true;
        } else {
            return false;
        }

    }

    @Override
    public void onResume() {
        super.onResume();
    }

    @Override
    public void onPause() {
        super.onPause();
    }

}

